#include "serialview.h"

GLParse *glParsec;
void recv_uart_farme(void)
{
    glParsec->getFrame();
}

SerialView::SerialView()
{

}


void SerialView::init(Ui::MainWindow *ui,GLParse *glParse)
{
    this->ui = ui;
    this->glParse = glParse;
    glParsec = glParse;

    connect(ui->pushButtonReflashSerial,SIGNAL(clicked()),this,SLOT(on_pushButtonReflashSerial_clicked()));
    connect(ui->pushButtonOpenSerial,SIGNAL(clicked()),this,SLOT(on_pushButtonOpenSerial_clicked()));

    connect(&serial.serialPort,SIGNAL(readyRead()),this,SLOT(readDataSlot()));
    connect(&serial.serialPort,SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(errorSlot(QSerialPort::SerialPortError)));

    //扫描串口
    reflashSerialList(serial.getSerialList());

    //波特率初始化
    ui->comboBoxBaud->addItems(serial.getBaudList());

    loadSettings();

    testThread.init(glParse);
    //testThread.start();

    ui->pushButtonFlashStart->setEnabled(false);
}

void SerialView::loadSettings()
{
    QString serialPortStr = settings->value("serial_port").toString();
    QString serialbaudStr = settings->value("serial_baud").toString();

    //qDebug() << serialPortStr;
    //qDebug() << serialbaudStr;

    int index = ui->comboBoxSerialList->findText(serialPortStr);
    if(index >= 0)
    {
        ui->comboBoxSerialList->setCurrentIndex(index);
    }

    index = ui->comboBoxBaud->findText(serialbaudStr);
    if(index >= 0)
    {
        ui->comboBoxBaud->setCurrentIndex(index);
    }

}

void SerialView::saveSettings()
{
    settings->setValue("serial_port",ui->comboBoxSerialList->currentText());
    settings->setValue("serial_baud",ui->comboBoxBaud->currentText());
}

void SerialView::reflashSerialList(QList<QSerialPortInfo> serialList)
{
    serialPortInfo = serialList;
    ui->comboBoxSerialList->clear();
    if(serialList.isEmpty())
    {
        mlog.show("未发现串口");
        return;
    }else{

        foreach (QSerialPortInfo info, serialList) {
            QString str = info.portName() + " " + info.manufacturer();
            ui->comboBoxSerialList->addItem(str);
        }
    }
}

void SerialView::on_pushButtonReflashSerial_clicked()
{
    //扫描串口
    reflashSerialList(serial.getSerialList());
    loadSettings();
}

void SerialView::on_pushButtonOpenSerial_clicked()
{
    if(ui->pushButtonOpenSerial->text().compare(QString::fromUtf8("打开")) == 0)
    {

        if(serialPortInfo.isEmpty())
        {
            mlog.show("当前无选择串口");
            return;
        }

        int index = ui->comboBoxSerialList->currentIndex();
        QString baud = ui->comboBoxBaud->currentText();
        if(serial.open(serialPortInfo[index],baud) == Serial::OK)
        {
            setOpenView();
        }else{
            mlog.show("无法打开串口");
        }
    }else{

        serial.close();
        setCloseView();
    }
}

void SerialView::setOpenView()
{
    mlog.show("串口打开成功");
    ui->pushButtonOpenSerial->setText("关闭");
    ui->pushButtonReflashSerial->setEnabled(false);
    ui->comboBoxBaud->setEnabled(false);
    ui->comboBoxSerialList->setEnabled(false);
    ui->pushButtonFlashStart->setEnabled(true);
    saveSettings();
    testThread.setFlag(true);
}

void SerialView::setCloseView()
{
    mlog.show("已关闭串口");
    ui->pushButtonOpenSerial->setText("打开");
    ui->pushButtonReflashSerial->setEnabled(true);
    ui->comboBoxBaud->setEnabled(true);
    ui->comboBoxSerialList->setEnabled(true);
    ui->pushButtonFlashStart->setEnabled(false);
    testThread.setFlag(false);
}

void SerialView::serialRecvData(QByteArray recv)
{
    qDebug() << recv.toHex();
}

void SerialView::readDataSlot()
{
    QByteArray recv = serial.serialPort.readAll();
    qDebug()  << "serial recv :" << EasyTool::ByteArrayToHexString(recv);

    glParse->receive_uart_data(recv.data(),recv.length());
}

void SerialView::errorSlot(QSerialPort::SerialPortError error)
{
    switch(error)
    {
    case QSerialPort::NoError:
        break;
    case QSerialPort::ResourceError:
    case QSerialPort::DeviceNotFoundError:
        serial.close();
        setCloseView();
        break;
    default:
        break;

    }

    qDebug() << error;
}



